package com.blogspot.sankdadevildaru.filelisthandler;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;

public class FileListBuilder {
	
	private String dirs[];
	public static final String OUTPUT_FILENAME = "C:\\FileList\\fileList";
	
	public FileListBuilder(){
		dirs[0] = "/";
	}
	
	public FileListBuilder(String []dir){
		dirs = dir;
	}
	
	public void createFileList() throws FileNotFoundException, IOException {
		
		PrintWriter pw = new PrintWriter(OUTPUT_FILENAME + System.currentTimeMillis() + ".txt");
		
		for(int i=0; i<dirs.length; i++)
		{
			File f = new File(dirs[i]);
			if(!f.isDirectory())
				System.out.println("The file " + f.getName() + " is not a directory!");
			else
				recurseDirectory(dirs[i], pw);		
		}
		
		pw.close();
	}
	
	private void recurseDirectory(String dirName, PrintWriter pw) throws FileNotFoundException, IOException {
		
		File directory = new File(dirName);
		File files[]= directory.listFiles();
		
		for (int i=0; i<files.length; i++){
			if(files[i].isDirectory())
				recurseDirectory(files[i].getAbsolutePath(), pw);
			else{
				pw.println(files[i].getAbsolutePath() + "|" + files[i].lastModified());
			}
		}
			
	}
	
	public static void main(String args[]) throws FileNotFoundException, IOException {
		String dirs[] = {"C:\\Inetpub"};
		FileListBuilder flb = new FileListBuilder(dirs);
		flb.createFileList();
	}
}
